#region (c) 2014 Nguyễn, M. Hải  - All rights reserved
/*
Simplified BSD License (BSD)
Copyright (c) 2014, Nguyễn, M. Hải ( www.Minh-Hai.com ). All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this 
      list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, 
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;

using Thn.Presentation.Controls;

namespace Demo
{
    public partial class MainPage : UserControl
    {
        #region Run On UI demo
        private void btnRun_Click(object sender, RoutedEventArgs e)
        {
            //disable the button so that user won't be clicking too many times
            btnRun.IsEnabled = false;

            //start the long processing on a different thread
            ThreadStart ts = new ThreadStart(DoLongProcessingStuff);
            Thread t = new Thread(ts);
            t.Start();
        }

        void DoLongProcessingStuff()
        {
            for (int i = 0; i < 100; i++)
            {
                Thread.Sleep(100);

                //update the UI
                //the following block will cause exception
                //if we don't use the RunOnUI helper
                this.RunOnUI(() =>
                    {
                        btnRun.Content = string.Format("Processing {0} %", i);
                    });
            }

            //re-enable the button again
            //note that we are still inside a thread, let's rely on RunOnUI again
            this.RunOnUI(() =>
                {
                    btnRun.Content = "Run me";
                    btnRun.IsEnabled = true;
                });
        }
        #endregion

        #region Populate Table
        void PopulateTable()
        {
            tb.SetContent(0, 0, "Hello world");
            tb[0, 0].Background = new SolidColorBrush(Colors.Yellow);
            tb[0, 0].Foreground = new SolidColorBrush(Colors.Red);
            tb[0, 5].Foreground = new SolidColorBrush(Colors.Red);
            tb.GoTo(0, 1);
            for (int i = 0; i < 10; i++)
            {
                tb.Set("This is line " + i.ToString());
            }

            Random ran = new Random();
            tb.GoTo(1, 1);
            for (int i = 0; i < 10; i++)
            {
                tb.Set(ran.Next(int.MaxValue));
            }

            MathLabel ml = new MathLabel();
            ml.Text = "m^2^/m^3^";
            tb.SetContent(1, 3, ml);
            tb[1, 2].Border = null;
            tb.Render();
        }
        #endregion

        #region Constructor
        public MainPage()
        {
            InitializeComponent();
            this.Loaded += MainPage_Loaded;
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            PopulateTable();
            lblNumber.Value = 1234;
        }
        #endregion

    }
}
